package com.itheima.health.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.itheima.interfaces.BusinessService;
import com.itheima.resources.MessageConstant;
import com.itheima.resources.entity.Result;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("business")
public class BusinessController {

    @Reference
    private BusinessService businessService;

    @RequestMapping("getBusinessData.do")
    public Result getBusinessData() {
        try {
            Map map = businessService.getBusinessData();
            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }


    }

    @RequestMapping("downloadBusinessDataToExcel.do")
    public void downloadBusinessDataToExcel(HttpServletResponse response, HttpSession session) {
        //   1.  加载 excel模板
        // 2   获取运营数据  封装 指定 Map 里面

        try {
            Map dbmap = businessService.getBusinessData();
            //  类加载器对象获取 classess 目录下的文件
            URL resource = this.getClass().getClassLoader().getResource("static/template/report_template.xlsx");
            String filepath = resource.getPath();
            System.out.println("===" + filepath);
            TemplateExportParams params = new TemplateExportParams(
                    filepath);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("curdate", dbmap.get("reportDate"));
            map.put("curdatemember", dbmap.get("todayNewMember"));
            map.put("curweekmember", dbmap.get("thisWeekNewMember"));
            map.put("totalmemebers", dbmap.get("totalMember"));
            map.put("curmonthmemebers", dbmap.get("thisMonthNewMember"));
            map.put("curorders", dbmap.get("todayOrderNumber"));
            map.put("curdateordereds", dbmap.get("todayVisitsNumber"));
            map.put("curweekorders", dbmap.get("thisWeekOrderNumber"));
            map.put("curmonthorders", dbmap.get("thisMonthOrderNumber"));
            map.put("curweekordereds", dbmap.get("thisWeekVisitsNumber"));
            map.put("curmonthordereds", dbmap.get("thisMonthVisitsNumber"));

            List<Map> excelMap = (List<Map>) dbmap.get("hotSetmeal");  //   热门套餐


            List<Map> listMap = new ArrayList<Map>();

            int size = excelMap.size();
            long totalsum = 0;
            for (int i = 0; i < size; i++) {
                Map lm = new HashMap();
                lm.put("name", excelMap.get(i).get("name"));
                Long setmeal_count = (Long) excelMap.get(i).get("setmeal_count");
                totalsum += setmeal_count;
                lm.put("counts", setmeal_count);
                lm.put("zhanbi", excelMap.get(i).get("proportion"));
                lm.put("remark", excelMap.get(i).get("remark"));
                listMap.add(lm);
            }
            map.put("totalcounts", totalsum);
            map.put("list", listMap);
            //  3  response 下载    一个流 2个 头  附件头 +  mime 头
            String downloadFileName = "report_template.xlsx";
            String mimeType = session.getServletContext().getMimeType(downloadFileName);
            response.setContentType(mimeType); //   文件类型的头   通知浏览器  下载的数据类型  excel文件

            response.setHeader("Content-Disposition", "attachment;filename=" + downloadFileName);//  告知浏览器 附件形式下载

            Workbook workbook = ExcelExportUtil.exportExcel(params, map);

            workbook.write(response.getOutputStream());//   浏览器下载的输出流
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
